/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import {
  insertText, // 数据中心，快宝驿站列表
  checkTemplate,
  saveTemplate,
  addKeyWordReply,
  getKeyWordReplyList,
  delKeyWordReply,
  editKeyWordReply,
  testKeyWordReply,
  getSubscribeReply,
  editSubscribeReply,
} from "@/services/platform";
import { isLegalData } from "@/utils/utils";

export default {
  namespace: "massPush",

  state: {
    templateData: {},
  },

  effects: {
    // 群发推送，插入网页链接
    *insertText({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(insertText, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },

    // 群发推送，检查是否有替换模板
    *checkTemplate({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(checkTemplate, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            templateData: data,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 群发推送，保存替换模板
    *saveTemplate({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(saveTemplate, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 群发推送，添加关键词回复
    *addKeyWordReply({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addKeyWordReply, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 群发推送，获取关键词回复列表
    *getKeyWordReplyList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getKeyWordReplyList, payload);
      if (!response) return;
      const { code, data = [], msg } = response;

      if (code == 0) {
        __dva_resolve(isLegalData(data));
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 群发推送，删除关键词回复
    *delKeyWordReply({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(delKeyWordReply, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        __dva_resolve();
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 群发推送，删除关键词回复
    *editKeyWordReply({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editKeyWordReply, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        __dva_resolve();
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 群发推送，测试关键词回复
    *testKeyWordReply({ payload }, { call }) {
      const response = yield call(testKeyWordReply, payload);
      if (!response) return;
      const { msg } = response;
      message.info(msg);
    },
    // 群发推送，编辑被关注回复内容
    *editSubscribeReply({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editSubscribeReply, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        __dva_resolve();
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 群发推送，被关注回复内容
    *getSubscribeReply({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getSubscribeReply, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
